//
//  TestFMDBOperationViewController.m
//  高仿暴走斗图
//
//  Created by key on 16/10/14.
//  Copyright © 2016年 huangsongyao. All rights reserved.
//

#import "TestFMDBOperationViewController.h"
#import "UITableViewCell+Property.h"

typedef NS_ENUM(NSUInteger, kFMDBOperationType) {
    
    kFMDBOperationTypeInsert,
    kFMDBOperationTypeDelete,
    kFMDBOperationTypeQuery,
    kFMDBOperationTypeModify,
    kFMDBOperationTypeCreateTable,
    kFMDBOperationTypeClearTable,
    kFMDBOperationTypeInsertTransaction,
};

@interface TestFMDBOperationViewController ()

@property (nonatomic, strong) NSMutableArray *operations;

@end

@implementation TestFMDBOperationViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.isCanReflesh = YES;
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.operations = [@[
                         
                         @{
                             @(kFMDBOperationTypeInsert) : @"插入",
                             },
                         @{
                             @(kFMDBOperationTypeDelete) : @"删除",
                             },
                         @{
                             @(kFMDBOperationTypeQuery) : @"索引",
                             },
                         @{
                             @(kFMDBOperationTypeModify) : @"修改",
                             },
                         @{
                             @(kFMDBOperationTypeCreateTable) : @"创建表单",
                             },
                         @{
                             @(kFMDBOperationTypeClearTable) : @"清空表单",
                             },
                         @{
                             @(kFMDBOperationTypeInsertTransaction) : @"事务插入",
                             },
                         
                         ] mutableCopy];
    
    @weakify(self);
    self.selectIndexPathCell = ^(UITableViewCell *tableViewCell, NSIndexPath *indexPath) {
        
        @strongify(self);
        NSDictionary *operation = tableViewCell.object;
        [self databaseOperationForType:(kFMDBOperationType)[operation.allKeys.firstObject integerValue]];
    };

}

- (void)databaseOperationForType:(kFMDBOperationType)type {
    
    switch (type) {
        case kFMDBOperationTypeInsert: {
            
            HSYFMDBOperationInfo *operation = [FMDBOperationManager createDatabaseOperationInfoForTableName:kFMDB_DATABASE_LIST_NAME fieldParams:[FMDBOperationManager testTableByFields] insertDatas:[@[
                                                                                                                                                                                                         @"第一个字段的第二次值",
                                                                                                                                                                                                         @"第二个字段的第二次值",
                                                                                                                                                                                                         @"第三个字段的第二次值",] mutableCopy]];
            [[FMDBOperationManager shareInstance].fmdbOperation fmdb_insertDataForOperationInfo:operation completed:^(BOOL result) {
                
            }];
        }
            break;
            
        case kFMDBOperationTypeDelete: {
            
            [[FMDBOperationManager shareInstance].fmdbOperation fmdb_deleteRowDataForTableName:kFMDB_DATABASE_LIST_NAME deleteValue:@"第一个字段的第二次值" whereField:kFMDB_FIELD_1 completed:^(BOOL result) {
                
            }];
        }
            break;
            
        case kFMDBOperationTypeQuery: {
            
            HSYFMDBOperationInfo *first = [HSYFMDBOperationInfo createDataBaseTableForName:kFMDB_DATABASE_LIST_NAME fields:[[FMDBOperationManager createDatabaseOpeartionFieldInfoForFieldParams:[FMDBOperationManager testTableByFields]] mutableCopy]];
            [[FMDBOperationManager shareInstance].fmdbOperation fmdb_queryAllDataForOperationInfo:first completed:^(NSMutableArray *result) {
                NSLog(@"%@", result);
            }];
            
//            [[FMDBOperationManager shareInstance].fmdbOperation fmdb_queryDataForOperationInfo:first whereField:@"field1" whereContent:@"第一个字段的值" completed:^(NSMutableArray *result) {
//                NSLog(@"%@", result);
//            }];
        }
            break;
            
        case kFMDBOperationTypeModify: {
            
            [[FMDBOperationManager shareInstance].fmdbOperation fmdb_modifyDataForTableName:kFMDB_DATABASE_LIST_NAME
                                                                                updateField:kFMDB_FIELD_2
                                                                              updateContent:@"现在更新啦！"
                                                                                 whereField:kFMDB_FIELD_1
                                                                               whereContent:@"第一个字段的值"
                                                                                  completed:^(BOOL result) {
                                                                                      NSLog(@"%d", result);
                                                                                  }];
        }
            break;
            
        case kFMDBOperationTypeCreateTable: {
            
        }
            break;
            
        case kFMDBOperationTypeClearTable: {
            
            [[FMDBOperationManager shareInstance].fmdbOperation fmdb_clearDataToTableName:kFMDB_DATABASE_LIST_NAME
                                                                                completed:^(BOOL result) {
                                                                                    NSLog(@"%d", result);
                                                                                }];
        }
            break;
            
        case kFMDBOperationTypeInsertTransaction: {
            
        }
            break;
            
        default:
            break;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *Identifier = @"Identifiers";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:Identifier];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:Identifier];
    }
    
    NSDictionary *operation = self.operations[indexPath.row];
    cell.object = operation;
    cell.textLabel.text = operation.allValues.firstObject;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    if (self.operations.count > 0) {
        return self.operations.count;
    }
    return 0;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 1;
}


@end
